home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / makebuf < prev    next >
Text File  |  1993-12-07  |  2KB  |  77 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified for GNU iostream by Per Bothner 1991.
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "%W% (Berkeley) %G%";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include "ioprivate.h"
  25. #include "fstream.h"
  26.  
  27. extern "C"
  28.   {
  29.   #include <sys/types.h>
  30.   #include <sys/stat.h>
  31.   }
  32.  
  33.  
  34. /*
  35.  * Allocate a file buffer, or switch to unbuffered I/O.
  36.  * Per the ANSI C standard, ALL tty devices default to line buffered.
  37.  *
  38.  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
  39.  * optimisation) right after the _fstat() that finds the buffer size.
  40.  */
  41. int filebuf::doallocate()
  42. {
  43.     register size_t size, couldbetty;
  44.     register char *p;
  45.     struct stat st;
  46.  
  47.     if (fd() < 0 || _fstat(fd(), &st) < 0) {
  48.     couldbetty = 0;
  49.     size = _G_BUFSIZ;
  50. #if 0
  51.     /* do not try to optimise fseek() */
  52.     fp->_flags |= __SNPT;
  53. #endif
  54.     } else {
  55.     couldbetty = S_ISCHR(st.st_mode);
  56. #if _G_HAVE_ST_BLKSIZE
  57.     size = st.st_blksize <= 0 ? _G_BUFSIZ : st.st_blksize;
  58. #else
  59.     size = _G_BUFSIZ;
  60. #endif
  61.     }
  62. #ifdef USE_MALLOC_BUF
  63.     if ((p = malloc(size)) == NULL) {
  64.     unbuffered(1);
  65. //    fp->_bf._base = fp->_p = fp->_nbuf;
  66. //    fp->_bf._size = 1;
  67.     return EOF;
  68.     }
  69. #else
  70.     p = ALLOC_BUF(size);
  71. #endif
  72.     setb(p, p+size, 1);
  73.     if (couldbetty && _isatty(fd()))
  74.     _flags |= _S_LINE_BUF;
  75.     return 1;
  76. }
  77.